package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmploymentStatus;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.EmploymentStatusForm;
import com.edu.hre.struts.form.admin.EmploymentStatusListForm;

public class EmploymentStatusDAOService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "employmentStatusDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmploymentStatusForm> getEmploymentStatusListInfo(int limit, int offset){

		List<HreEmploymentStatus> hreEmploymentStatus = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreEmploymentStatus.class);
		ArrayList<EmploymentStatusForm> employmentStatuses = new ArrayList<EmploymentStatusForm>();
		
		if (hreEmploymentStatus == null) {
			return employmentStatuses;
		}
		for (HreEmploymentStatus hreStatus: hreEmploymentStatus) {
			
			try {

				EmploymentStatusForm status = new EmploymentStatusForm();
				status.setId(hreStatus.getId() + "");
				status.setEmploymentStatus(CommonUtils.normalizeString(hreStatus.getName()));

				employmentStatuses.add(status);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return employmentStatuses;
	}
	
	public int countEmploymentStatus(){

		int count = baseService.countByColumns(null, null, null, "id", HreEmploymentStatus.class);
		return count < 0 ? 0 : count;
	}

	public void deleteEmploymentStatus(EmploymentStatusListForm form){

		ArrayList<EmploymentStatusForm> info = (ArrayList<EmploymentStatusForm>) form.getAllStatus();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmploymentStatusForm status = info.get(i);
				if (status.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(status.getId()), null, null, 0, HreEmploymentStatus.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
